package com;

import java.time.LocalDateTime;
import java.util.List;

public class Admin extends User {
    public Admin(String userId, String username, String password,
                 LocalDateTime registerTime, String phone) {
        super(userId, username, password, registerTime, "管理员", phone);
    }
    @Override
    public boolean login(String inputUsername, String inputPassword) {
        return this.getUsername().equals(inputUsername) && this.getPassword().equals(inputPassword);
    }
    public void resetUserPassword(User user, LogService logService) {
        if (user instanceof Admin) {
            System.out.println("错误：不能重置管理员密码");
            return;
        }
        user.setPassword("123456");
        logService.recordLog("重置密码", this, "重置"
                + user.getUserType() + "[" + user.getUsername() + "]的密码");
    }
    public boolean addUser(User newUser, List<User> userList, LogService logService) {
        for (User u : userList) {
            if (u.getUserId().equals(newUser.getUserId())
                    || u.getUsername().equals(newUser.getUsername())
                    || u.getPhone().equals(newUser.getPhone())) {
                System.out.println("错误：用户ID/用户名/手机号已存在");
                return false;
            }
        }
        userList.add(newUser);
        logService.recordLog("添加用户", this, "新增" + newUser.getUserType() + "[" + newUser.getUsername() + "]");
        return true;
    }
    public boolean deleteUser(String userId, List<User> userList, LogService logService) {
        for (User u : userList) {
            if (u.getUserId().equals(userId)) {
                if (u.getUsername().equals("admin")) {
                    System.out.println("错误：不能删除admin账户");
                    return false;
                }
                userList.remove(u);
                logService.recordLog("删除用户", this,
                        "删除" + u.getUserType() + "[" + u.getUsername() + "]");
                return true;
            }
        }
        System.out.println("错误：用户不存在");
        return false;
    }
    public User queryUser(String key, String value, List<User> userList) {
        for (User u : userList) {
            if (("userId".equals(key) && u.getUserId().equals(value))
                    || ("username".equals(key) && u.getUsername().equals(value))) {
                return u;
            }
        }
        return null;
    }}